﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

public class CApCableEnabler : ITxCommandEnabler
{
  private readonly TxTcResourceCreationDataEx m_tcResourceCreationData;
  private const string TC_CABLE_RESOURCE_TYPE_LEGACY = "Mfg0MEEquipmentRevision";
  private const string TC_CABLE_RESOURCE_TYPE = "Mfg0CableRevision";

  public CApCableEnabler()
  {
    if (!TxTcUtilities.IsTcAnyPlatform)
      return;
    this.m_tcResourceCreationData = new TxTcResourceCreationDataEx("Cable", this.GetTCCableResourceType);
  }

  private bool ShouldEnable()
  {
    bool flag1 = true;
    if (!TxApplication.ActiveDocument.PlatformGlobalServicesProvider.IsDataLoaded())
      flag1 = false;
    else if (TxTcUtilities.IsTcAnyPlatform)
    {
      flag1 = false;
      TxObjectList items = TxApplication.ActiveSelection.GetItems();
      if (((Collection<ITxObject>) items).Count == 1)
      {
        flag1 = ((Collection<ITxObject>) items)[0].CanBeModified;
        if (flag1)
        {
          bool flag2 = this.IsResourceTypeDefinedAsInstance() && this.IsScopeValidForCable(((Collection<ITxObject>) items)[0]);
          flag1 = !TxFeatureManager.CheckFeature("CABLE_STIFFNESS_OPTIMIZER") ? flag2 || this.IsCableObject(((Collection<ITxObject>) items)[0]) : flag2 || this.IsCableObject(((Collection<ITxObject>) items)[0]) || this.IsPointCloudLayerObject(((Collection<ITxObject>) items)[0]);
        }
      }
    }
    return flag1;
  }

  private bool IsCableObject(ITxObject selectedObj) => selectedObj is ITxCable;

  private bool IsPointCloudLayerObject(ITxObject selectedObj) => selectedObj is ITxPointCloudLayer;

  private bool IsScopeValidForCable(ITxObject selectedScope)
  {
    bool flag = false;
    if (selectedScope is ITxObjectCollection objectCollection)
      flag = TxTcUtilities.CanCreateTcResource((ITxObject) objectCollection, this.m_tcResourceCreationData);
    return flag;
  }

  private bool IsResourceTypeDefinedAsInstance()
  {
    return TxTcUtilities.IsResourceInstanceType("Mfg0CableRevision");
  }

  public bool Enable
  {
    get
    {
      if (!this.ShouldEnable() || this.IsCableDialogOpened || this.IsSimulationRunning)
        return false;
      return TxApplication.ActiveDocument.CurrentStudy != null || TxCollisionRoot.op_Inequality(TxApplication.ActiveDocument.CollisionRoot, (ITxObject) null);
    }
  }

  private bool IsSimulationRunning
  {
    get => TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning();
  }

  private string GetTCCableResourceType
  {
    get
    {
      return TxApplication.ActiveDocument.PlatformGlobalServicesProvider.GetTypeMetaData("Mfg0CableRevision") != null ? "Mfg0CableRevision" : "Mfg0MEEquipmentRevision";
    }
  }

  public bool IsCableDialogOpened { get; set; }
}
